
#include "CInput.h"

/************************************************************************/
/* Name:		CInput													*/
/* Description:	define variables and call init functions				*/
/************************************************************************/

CInput::CInput(HWND hwnd)
{
m_pDIObject = NULL;
m_pDIKeyboardDevice = NULL;

InitDirectInput(hwnd);
InitKeyboard(hwnd);
}//CInput

/************************************************************************/
/* Name:		~CInput													*/
/* Description:	release DirectInput object and devices					*/
/************************************************************************/

CInput::~CInput()
{
if(m_pDIKeyboardDevice != NULL)
 {
 m_pDIKeyboardDevice->Unacquire();
 m_pDIKeyboardDevice->Release();
 m_pDIKeyboardDevice = NULL;
 }

if(m_pDIObject != NULL)
 {
 m_pDIObject->Release();
 m_pDIObject = NULL;
 }
}//~CInput

/************************************************************************/
/* Name:		InitDirectInput											*/
/* Description:	create DirectInput object								*/
/************************************************************************/

bool CInput::InitDirectInput(HWND hwnd)
{
if(FAILED(DirectInput8Create(GetModuleHandle(NULL),DIRECTINPUT_VERSION,IID_IDirectInput8,(void**)&m_pDIObject,NULL)))
 {
 MessageBox(hwnd,"DirectInput8Create() failed!","InitDirectInput()",MB_OK);
 return false;
 }

return true;
}//InitDirectInput

/************************************************************************/
/* Name:		InitKeyboard											*/
/* Description:	init keyboard device									*/
/************************************************************************/

bool CInput::InitKeyboard(HWND hwnd)
{
	if(FAILED(m_pDIObject->CreateDevice(GUID_SysKeyboard,&m_pDIKeyboardDevice,NULL)))
	 {
	 MessageBox(hwnd,"CreateDevice() failed!","InitKeyboard()",MB_OK);
	 return false;
	 }

	if(FAILED(m_pDIKeyboardDevice->SetDataFormat(&c_dfDIKeyboard)))
	 {
	 MessageBox(hwnd,"SetDataFormat() failed!","InitKeyboard()",MB_OK);
	 return false;
	 }

	if(FAILED(m_pDIKeyboardDevice->SetCooperativeLevel(hwnd,DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
	 {
	 MessageBox(hwnd,"SetCooperativeLevel() failed!","InitKeyboard()",MB_OK);
	 return false;
	 }

	if(FAILED(m_pDIKeyboardDevice->Acquire()))
	 {
	 MessageBox(hwnd,"Acquire() failed!","InitKeyboard()",MB_OK);
	 return false;
	 }

	return true;
}//InitKeyboard

/************************************************************************/
/* Name:		Update													*/
/* Description:	update state of keyboard device							*/
/************************************************************************/

bool CInput::Update(HWND hwnd)
{
if(FAILED(m_pDIKeyboardDevice->GetDeviceState(sizeof(KeyBuffer),(LPVOID)&KeyBuffer)))
 {
 MessageBox(hwnd,"GetDeviceState() failed!","Update()",MB_OK);
 return false;
 }

return true;
}//Update

/************************************************************************/
/* Name:		KeyPressed												*/
/* Description:	check if key has been pressed							*/
/************************************************************************/

bool CInput::KeyPressed(int Key)
{
if(KeyBuffer[Key] & 0x80)
 {
 return true;
 }

return false;
}//KeyPressed